﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Frame.Lib.Core.Tree;
using Frame.Lib.Core.Dept;
using Frame.Lib.Enums;
using Frame.Lib.Utilities;
using System.Collections.Generic;
using System.Text;
using Frame.Lib.Core;

public partial class Admin_User_DeptTree : PageBase
{

    private TreeBO<DeptInfo> _tree;
    private TreeBO<DeptInfo> Tree 
    {
        get
        {
            if (_tree == null)
            {
                _tree = new TreeBO<DeptInfo>(STableName.Dept);
            }
            return _tree;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {        
        if (!IsPostBack)
        {
            HtmlGenericControl body = (HtmlGenericControl)this.Master.Page.Form.FindControl("body");
            body.Attributes.Add("class", "left_body_bg");
            BindTree();
            JSUtil.SetDivHeight("tree", 30);
        }
    }

    private void BindTree()
    {
        int rootID = (int)Tree.GetRootID();

        IEnumerable<DeptInfo> list = Tree.GetList().Where(T => T.Path.Contains("," + rootID + ","));
        StringBuilder sb = new StringBuilder();

        foreach (DeptInfo info in list)
        {
            sb.Append("{ id: " + info.ID + ", pId: " + info.UpperID + ", name: '" + info.Name + "', open: false , iconSkin: 'folder' , encryptID :'" + this.Encrypt(info.ID) + "'},");
        }

        lblJson.Text = sb.ToString().TrimEnd(',');

        string navigateUrl = "List.aspx?DeptID=";
        lnkRoot.NavigateUrl = navigateUrl + StrUtil.EncryptUrl(rootID);
        lnkRoot.Text = lnkRoot.ToolTip = Tree.GetName(rootID);
        if (lnkRoot.Text.Length > 12)
        {
            lnkRoot.Text = lnkRoot.Text.Substring(0, 12);
        }
    

       // Tree.BindTreeView(tv,null,null, rootID, navigateUrl);
    }
}
